/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sunsite.service;

import java.util.Date;
import java.util.Locale;
import org.apache.struts2.ServletActionContext;
import org.hibernate.exception.DataException;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import sunsite.dao.VisitDao;
import sunsite.po.Usr;
import sunsite.po.Visit;
import sunsite.tools.ContextHelper;
import sunsite.tools.MD5_Past;
import sunsite.tools.Naming;
import sunsite.tools.ParamsString;
import sunsite.tools.UserIdentifier;
import sunsite.vo.UserInfo;

/**
 *
 * @author Administrator
 */
public class UserServiceImpl implements UserService {

    private sunsite.dao.UsrDao userDao;
    private sunsite.dao.VisitDao visitDao;

    private SimpleMailMessage mialMessage;
    private MailSender mailSender;
    private ResourceBundleMessageSource messageSource;

    public void setUserDao(sunsite.dao.UsrDaoImpl userDao) {
        this.userDao = userDao;
    }


    public void setVisitDao(VisitDao visitDao) {
        this.visitDao = visitDao;
    }

    @Override
    public Usr validate(String userName, String password) throws Exception{
        Usr user = userDao.getUsrByUserName(userName);
        if(user!=null && user.getPassword().equals(new sunsite.tools.MD5().getMD5ofStr(password)))
            return user;
        else
            return null;
    }

    @Override
    public int register(Usr user) throws Exception{
        if(user == null)
            return -1;
        try
        {
            user.setPassword(new sunsite.tools.MD5().getMD5ofStr(user.getPassword()));
            userDao.addUser(user);
            this.Login(user);
            return 1;
        }
        catch(Exception err)
        {
            throw err;
        }
    }

    @Override
    public void Login(Usr user) throws Exception{
        //ContextHelper.getSession().put(Naming.SessionName.UserSession, new UserInfo(user.getUserId(), user.getUserName(), user.getFullName(), user.getRole().getRoleId(), user.getRole().getRoleName()));
        String visitId = (String)(ContextHelper.getSession().get(ParamsString.VisitId));
        Visit visit = new Visit();
        visit.setVisitId(visitId);
        visitDao.updateVisit(visit);
        user.setLastLogin(new Date());
        user.setLoginCount(user.getLoginCount()+1);
        userDao.update(user);
    }

    @Override
    public void Login(UserInfo userInfo) throws Exception{
        Login(userDao.getUsrByUserId(userInfo.getUserId()));
    }

    @Override
    public boolean isUserExisted(String userName) throws Exception {
        Usr user = userDao.getUsrByUserName(userName);
        return user != null;
    }

    @Override
    public void reSetPassword(Usr user) throws MailException,Exception{
        String newPsw = UserIdentifier.newPassword(6);
        user.setPassword(new sunsite.tools.MD5().getMD5ofStr(newPsw));
        userDao.update(user);

        //可把通用的SimpleMailMessage信息写在ApplicationContext.xml，然后作为参数传给特定的SimpleMailMessage
        SimpleMailMessage message = new SimpleMailMessage(mialMessage);
        //也可完全重新设置，但要确保To和From的地址正确

        message.setTo(user.getEmail());
        message.setSubject(messageSource.getMessage("user.newPsw", null, Locale.getDefault()));
        message.setText(messageSource.getMessage("user.newPswTip", null, Locale.getDefault()) + newPsw);
        mailSender.send(message);
    }

    @Override
    public int changePassword(String oldPsw, String newPsw) throws Exception{
        UserInfo userInfo = (UserInfo)ContextHelper.getSession().get(Naming.SessionName.UserSession);
        if(userInfo == null)
            return -1;
        else
        {
            Usr user = userDao.getUsrByUserName(userInfo.getUserName());
            if(user.getPassword().equals(new sunsite.tools.MD5().getMD5ofStr(oldPsw)))
            {
                user.setPassword(new sunsite.tools.MD5().getMD5ofStr(newPsw));
                userDao.update(user);
                return 0;
            }
            else
                return 1;
        }
    }

    public void setMialMessage(SimpleMailMessage mialMessage) {
        this.mialMessage = mialMessage;
    }

    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }

    @Override
    public void delete(String userName) throws DataException {
        userDao.delete(userName);
    }

    @Override
    public Usr getUsrByName(String userName) {
        return userDao.getUsrByUserName(userName);
    }

    @Override
    public Usr validateEmail(String userName, String email) throws Exception
    {
        Usr user = userDao.getUsrByUserName(userName);
        if(user == null)
            return null;
        if(user.getEmail().equalsIgnoreCase(email))
            return user;
        return user;
    }

    /**
     * @param messageSource the messageSource to set
     */
    public void setMessageSource(ResourceBundleMessageSource messageSource)
    {
        this.messageSource = messageSource;
    }

    public void update(Usr user) throws Exception {
        this.userDao.update(user);
    }

}
